package com.qyhl.webtv.module_news.news.normal;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.MJavascriptInterface;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;

public class WebViewTestActivity extends AppCompatActivity {

    String html = "<html>\n" +
            "     <head>\n" +
            "      <style type=\"text/css\">img {max-width: 100%; }video::-webkit-media-controls-enclosure {overflow:hidden;}video::-webkit-media-controls-panel {width: calc(100% + 32px);}audio::-webkit-media-controls {overflow: hidden !important}audio::-webkit-media-controls-enclosure {width: calc(100% + 32px);margin-left: auto;}video{max-width: 100%;}audio{clear:both;display:block;margin:auto;max-width: 100%;}</style>\n" +
            "     </head>\n" +
            "     <body>\n" +
            "      <style type=\"text/css\">img {max-width: 100%; }video::-webkit-media-controls-enclosure {overflow:hidden;}video::-webkit-media-controls-panel {width: calc(100% + 32px);}video{max-width: 100%;}audio{clear:both;display:block;margin:auto;width: 100%;}</style>\n" +
            "      <p style=\"text-align: center;\"><img src=\"file:///mnt/sdcard/helan/picture/de34365d-77f1-4bc6-a7e8-e2b75ce7bc9b.png\" class=\"nomalImg\" alt=\"1_b2677c17ab1d42c3bccddc8d87e4f85b.jpg\" style=\"max-width:100% !important;max-height:auto !important\"></p>\n" +
            "      <p cms-style=\"font-L\" style=\"margin-top: 0px; margin-bottom: 18px; padding: 0px; font-size: 18px; color: rgb(77, 79, 83); font-family: &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, &quot;\\\\5FAE软雅黑&quot;, SimSun, &quot;\\\\5B8B体&quot;, Arial; letter-spacing: 1px; white-space: normal; background-color: rgb(255, 255, 255);\">　　10月31日，山东济南。杨勇抱着9个月大的女儿用3小时跑完21公里半马。杨勇说这已是今年第二次带女儿参加跑步比赛，想让她体验新鲜事物，感受大自然，长大挑战自我。</p>\n" +
            "      <p><span style=\"font-size: 18px;\"></span><br></p>\n" +
            "     </body>\n" +
            "    </html>";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_test);
        inintView();
    }

    private void inintView() {

        WebView webview = findViewById(R.id.webview);

        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("utf-8");
        settings.setDomStorageEnabled(true);
        settings.setBlockNetworkImage(false);//是否阻塞加载网络图片  协议http or https
        settings.setUseWideViewPort(false);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        webview.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
        webview.addJavascriptInterface(new MJavascriptInterface(this, StringUtils.returnImageUrlsFromHtml(html)), "imagelistener");

        Button btn = findViewById(R.id.news_button);
        btn.setOnClickListener(view -> RouterManager.router(ARouterPathConstant.SETTING));

    }
}